<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="bannerfile" label="图片文件" required>
				<uni-file-picker file-mediatype="image" return-type="object" v-model="formData.bannerfile">
				</uni-file-picker>
			</uni-forms-item>
			<uni-forms-item name="open_url" label="点击目标地址">
				<uni-easyinput placeholder="点击跳转目标地址。如果是web地址则使用内置web-view打开；如果是本地页面则跳转本地页面；如果是schema地址则打开本地的app"
					v-model="formData.open_url" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="title" label="标题">
				<uni-easyinput placeholder="请输入大标题内容" v-model="formData.title" trim="both"></uni-easyinput>
			</uni-forms-item>

			<uni-forms-item label="字体颜色" name="titleColor" labelWidth="120">
				<view class="color-box">
					<uni-easyinput placeholder="请设置大标题字体颜色" v-model="formData.titleColor" trim="both"
						class="color-value"></uni-easyinput>
					<view class="color-border">
						<view @click="isOpenTitleColor = !isOpenTitleColor" class="color-show"
							:style="{'background':formData.titleColor}">
							<uni-icons v-if="isOpenTitleColor" type="top" size="25"></uni-icons>
							<uni-icons v-else type="bottom" size="25"></uni-icons>
						</view>
					</view>
				</view>
				<v587-color-picker v-if="isOpenTitleColor" v-model="formData.titleColor"
					@onColorChange="pickerTitleColor" class="color-picker" />
			</uni-forms-item>


			<uni-forms-item name="subTitle" label="副标题">
				<uni-easyinput placeholder="请输入副标题内容" v-model="formData.subTitle" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item label="副标题字体颜色" name="subTitleColor" labelWidth="120">
				<view class="color-box">
					<uni-easyinput placeholder="请设置副标题字体颜色" v-model="formData.subTitleColor" trim="both"
						class="color-value"></uni-easyinput>
					<view class="color-border">
						<view @click="isOpenSubTitleColor = !isOpenSubTitleColor" class="color-show"
							:style="{'background':formData.subTitleColor}">
							<uni-icons v-if="isOpenSubTitleColor" type="top" size="25"></uni-icons>
							<uni-icons v-else type="bottom" size="25"></uni-icons>
						</view>
					</view>
				</view>
				<v587-color-picker v-if="isOpenSubTitleColor" v-model="formData.subTitleColor"
					@onColorChange="pickerSubTitleColor" class="color-picker" />
			</uni-forms-item>
			
			<uni-forms-item label="轮播背景色" name="bannerBackground" labelWidth="120">
				<view class="color-box">
					<uni-easyinput placeholder="请设置轮播背景色" v-model="formData.bannerBackground" trim="both"
						class="color-value"></uni-easyinput>
					<view class="color-border">
						<view @click="isOpenBannerBackground = !isOpenBannerBackground" class="color-show"
							:style="{'background':formData.bannerBackground}">
							<uni-icons v-if="isOpenBannerBackground" type="top" size="25"></uni-icons>
							<uni-icons v-else type="bottom" size="25"></uni-icons>
						</view>
					</view>
				</view>
				<v587-color-picker v-if="isOpenBannerBackground" v-model="formData.bannerBackground"
					@onColorChange="pickerBannerBackground" class="color-picker" />
			</uni-forms-item>
			
			<uni-forms-item name="sort" label="排序">
				<uni-easyinput placeholder="数字越小，排序越前" type="number" v-model="formData.sort"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="status" label="是否显示">
				<switch @change="binddata('status', $event.detail.value)" :checked="formData.status"></switch>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/sy-banner.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'sy-banner';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"bannerfile": null,
				"open_url": "",
				"title": "",
				"titleColor": "#ffffff",
				"subTitle": "",
				"subTitleColor": "#ffffff",
				"bannerBackground": "#ffffff",
				"sort": null,
				"status": true
			}
			return {
				isOpenTitleColor: false,
				isOpenSubTitleColor: false,
				isOpenBannerBackground: false,
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		watch: {
			// 使用watch来响应数据的变化
			'formData.titleColor'(newVal, oldVal) {
				if(!newVal){
					this.formData.titleColor = oldVal
				}
			},
			'formData.subTitleColor'(newVal, oldVal) {
				if(!newVal){
					this.formData.subTitleColor = oldVal
				}
			},
			'formData.bannerBackground'(newVal, oldVal) {
				if(!newVal){
					this.formData.bannerBackground = oldVal
				}
			}
		},
		methods: {
			pickerTitleColor(e) {
				this.formData.titleColor = e
			},
			pickerSubTitleColor(e) {
				this.formData.subTitleColor = e
			},
			pickerBannerBackground(e) {
				this.formData.bannerBackground = e
			},
			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				var pattern = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
				
				if(!pattern.test(value.titleColor) || !pattern.test(value.subTitleColor) || !pattern.test(value.bannerBackground)){
					uni.showToast({
						title: '色值格式不正确，请重新选择或输入',
						icon:'error'
					});
					return false
				}
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>
<style>
	.switch {
		color: #aaafff;
	}

	.color-box {
		display: flex;
		flex-direction: row;
		align-items: center;
		position: relative;
	}

	.color-value {
		margin-right: 40rpx;
	}

	.color-border {
		width: 50px;
		height: 50px;
		border: #ddd solid 2px;
		justify-content: center;
		align-items: center;
		display: flex
	}

	.color-show {
		width: 40px;
		height: 40px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.color-picker {
		position: absolute;
		top: 63px;
		left: 330px;
		z-index: 99;
	}
</style>
